package com.example.park

import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import androidx.recyclerview.widget.LinearLayoutManager
import com.example.base.mvvm.BaseActivity
import com.example.park.databinding.ActivityMainBinding

class MainActivity : BaseActivity<ActivityMainBinding,MainViewModel>() {
    lateinit var newsAdapter: NewsAdapter
    override fun initView() {
        newsAdapter = NewsAdapter()
        binding.rv.adapter = newsAdapter
        binding.rv.layoutManager = LinearLayoutManager(this)
        viewModel.news()

        newsAdapter.setOnItemClickListener { adapter, view, position ->
            val intent = Intent(this,UpdateActivity::class.java)
            intent.putExtra("id",newsAdapter.data[position].id)
            startActivity(intent)
        }
    }

    override fun initData() {
        viewModel.newsSuccess.observe(this){
            if(it.code==200){
                newsAdapter.data.clear()
                newsAdapter.data.addAll(it.data)
                newsAdapter.notifyDataSetChanged()
            }
        }
    }

    override fun getViewModel(): Class<MainViewModel>  = MainViewModel::class.java

    override fun getLayout(): Int  = R.layout.activity_main

}